//
//  BasePopView.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/14.
//

#import "BasePopView.h"

@implementation BasePopView

- (UIView*)contentView
{
    if (!_contentView) {
        _contentView = [[UIView alloc]init];
    }
    return _contentView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.contentView];
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint press = CGPointZero;
    for (UITouch * touch in touches) {
        press = [touch locationInView:self];
        break;
    }
    
    if (CGRectContainsPoint(self.contentView.frame, press) || press.x == 0 || press.y == 0) {
        [self.superview endEditing:YES];
    }else {
        if (self.touchNoRespod) {
            return;
        }
        [self closeView];
        return;
    }
}

- (void)closeView
{
    [UIView animateWithDuration:0.2 animations:^{
        switch (self.location) {
            case BottomStyle:{
                [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.bottom.equalTo(self.mas_bottom).offset((self.contentView.frame.size.height - self.hiddenHeight));
                }];
            }
                break;
            case CenterStyle:{
                self.contentView.transform = CGAffineTransformMakeScale(0.001f, 0.001f);
            }
                break;
            default:
                break;
        }
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.hidden = YES;
        self.isShow = NO;
    }];
}

- (void)startAnimation
{
    self.hidden = NO;
    self.isShow = YES;
    if (self.location == CenterStyle) {
        self.contentView.transform = CGAffineTransformMakeScale(0.0, 0.0);
    }
    float timeAnimation = self.contentView.getHeight/800.0;
    [UIView  animateWithDuration:timeAnimation animations:^{
        switch (self.location) {
            case BottomStyle:{
                [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.bottom.equalTo(self.mas_bottom).offset(self.hiddenHeight);
                }];
            }
                break;
            case CenterStyle:{
                self.contentView.transform = CGAffineTransformIdentity;
            }
                break;
            default:
                break;
        }
        [self layoutIfNeeded];
    }completion:^(BOOL finished) {
       
    }];
}

@end
